<?php
/**
 * Created by PhpStorm.
 * User: 技术员专用
 * Date: 2019/2/18
 * Time: 11:17
 */

namespace app\admin\controller;
use Hanson\Vbot\Foundation\Vbot;
use Illuminate\Cache\RedisTaggedCache;
use SebastianBergmann\GlobalState\Blacklist;
use Hanson\Vbot\Message\Text;
use app\admin\controller\Observer;
use app\admin\controller\MessageHandler;
use think\Controller;

class Welogin extends Controller
{
    public function index(){
        $config=config('vbot');
        $vbot=new Vbot($config);
        $vbot->messageHandler->setHandler([MessageHandler::class,'messageHandler']);
        //加载插件
//        $vbot->messageExtension->load([
//            Blacklist::class,
//            GuessNumber::class,
//        ]);
        $vbot->observer->setQrCodeObserver([Observer::class,'setQrCodeObserver']);

        $vbot->observer->setLoginSuccessObserver([Observer::class,'setLoginSuccessObserver']);

        $vbot->observer->setReLoginSuccessObserver([Observer::class, 'setReLoginSuccessObserver']);

        $vbot->observer->setExitObserver([Observer::class, 'setExitObserver']);

        $vbot->observer->setFetchContactObserver([Observer::class, 'setFetchContactObserver']);

        $vbot->observer->setBeforeMessageObserver([Observer::class, 'setBeforeMessageObserver']);

        $vbot->observer->setNeedActivateObserver([Observer::class, 'setNeedActivateObserver']);

//        $vbot->messageHandler->setHandler(function ($message){
//            Text::send($message['form']['UserName'],'I am Vbot');
//        });
        $vbot->server->serve();
    }
    public function botStart(){
//        echo 'success';
//        fastcgi_finish_request();
        system("php /www/wwwroot/vbottest/public/index.php admin/webot/run/com/start");
    }

    public function getUrl(){
        $link=db('qrcode')->where(['status'=>1])->order('id desc')->find();
        if (!empty($link)){
            return $this->success('获取成功','',$link,'');
        }else{
            return $this->error('获取失败');
        }
    }
    public function start(){
        return $this->fetch('start');
    }
    public function qrcode(){
        return $this->fetch();
    }
    public function test(){
        $asd=' "厦门58同城合伙人"邀请你加入群聊58合伙人运营对接群，进入可查看详情。';
        print_r(strripos($asd,'加入群聊'));
        print_r(substr($asd,strripos($asd,'加入群聊')));
        print_r(str_replace('加入群聊','',substr($asd,strripos($asd,'加入群聊'))));
    }

}